#!/usr/bin/perl -w

###############################################################################
# Transform the annotation input files from the affymetrix platform
# into a database load file for the anno table in the database
#
# Parameters needed: dbname, username, anno_files, temp_dir
# Output files: temp_dir/anno.out
#
# Author: Herodotos Herodotou and Nedyalko Borisov
# Date: March 11, 2010
#
##############################################################################

use getOpts qw(get_options println);
use allele_convertion qw(convert_alleles flip_allele get_chromosome_number);
sub remove_quotes;

if ($#ARGV != 0)
{
   println qq(Please specify the .properties file);
   println qq(Usage: perl $0 load.properties);
   exit(-1);
}

# Get input parameters
%opt = get_options($ARGV[0]);
$DB_NAME  =$opt{'dbname'};
$USERNAME =$opt{'username'};
$ANNO_FILES = $opt{'anno_files'};
$TEMP_DIR = $opt{'temp_dir'};

# Open output file
open OUTFILE, ">", $TEMP_DIR."/anno.out" or die $!;

# Create the manuf_ids reference file
$query = qq(SELECT manuf_id FROM anno WHERE platform = 'a');
!system qq(psql -U $USERNAME -d $DB_NAME ).
        qq(-c "$query" -t -o $TEMP_DIR/anno_ref) or die $!;

# Load the reference file in the anno map
open INFILE, "<", "$TEMP_DIR/anno_ref" or die $!;

%manufids = ();
while ($line = <INFILE>)
{
   $line =~ s/^\s+//;
   $line =~ s/\s+$//;
   $manufids{$line} = '';
}

close INFILE;
!system qq(rm -f $TEMP_DIR/anno_ref) or die $!;


# Perform the transformation
@annofiles = split(/;/, $ANNO_FILES);

foreach (@annofiles)
{
    $annofile = $_;
    $annofile =~ s/\s+$//;
    $num_records = 0;

    # Open input anno files
    println qq(Transform anno file: $annofile);
    open INFILE, "<", $annofile or die $!;

    # Find the header line
    while($line = <INFILE>)
    {
       if ($line !~ "^#")
       {
          last;
       }
    }

    # Process the data
    while($line = <INFILE>)
    {
       # Get the data
       @data = split(/,/, $line);
       $manuf_id = remove_quotes($data[0]);
       $rsid = remove_quotes($data[1]);
       $chromosome = get_chromosome_number(remove_quotes($data[2]));
       $location = remove_quotes($data[3]);
       $location = ($location =~ /---/) ? "-9" : $location;

       # Make sure this manuf id is not already in the database   
       if (exists $manufids{$manuf_id})
       {
          next;
       }

       # Find the alleles
       $alleleA = remove_quotes($data[8]);
       $alleleB = remove_quotes($data[9]);
       $reverse = ($data[18] =~ /reverse/) ? "true" : "false";
           
       # Print anno
       print OUTFILE qq($manuf_id,a,$rsid,$chromosome,$location,$alleleA,$alleleB,$reverse\n);
       ++$num_records;
    }
    
    # Close file
    close INFILE;
    println qq(Done anno file transformation - produced $num_records records);
}

# Done
close OUTFILE;


###############################################################################
# Function: remove_quotes
#
# Input: a quoted string
#
# Description: Removes the quotes from a string
#
##############################################################################
sub remove_quotes()
{
   my $str = $_[0];
   $str =~ s/\"(.*)\"/$1/;
   return $str;
}
